<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>test</title>
</head>
<body>
	<canvas id="canvas"></canvas>
	<script type="x-shader/x-vertex" id="vertex">
		attribute vec2 a_position;
		uniform bool isB;
		attribute vec2 a_position2;
		void main(){
			if(isB){

				gl_Position = vec4(a_position.x+0.5,a_position2.x,1.,1.);
			}else{
				
			gl_Position = vec4(a_position.x,a_position2.x,1.,1.);
			}
		}
	</script>
	<script type="x-shader/x-fragment" id="fragment">
		void main(){
			gl_FragColor = vec4(1.0,0.0,1.0,1.0);
		}
	</script>
	<script src="https://webglfundamentals.org/webgl/resources/webgl-utils.js"></script>
	<script src="https://webglfundamentals.org/webgl/resources/m4.js"></script>
	<script src="https://webglfundamentals.org/webgl/resources/primitives.js"></script>
	<script src="https://webglfundamentals.org/webgl/resources/texture-utils.js"></script>
	<script src="https://webglfundamentals.org/webgl/resources/chroma.min.js"></script>
	<script>
		/** @type {HTMLCanvasElement} */   
		const canvas=document.getElementById("canvas")
		const gl=canvas.getContext("webgl")
		const programInfo=webglUtils.createProgramInfo(gl,["vertex","fragment"])
		gl.useProgram(programInfo.program)
		const a_position=gl.getAttribLocation(programInfo.program,"a_position")
		const a_position2=gl.getAttribLocation(programInfo.program,"a_position2")
		const isB=gl.getUniformLocation(programInfo.program,"isB")
		const buffer=gl.createBuffer()
		const positions=[
			-.8,0,  
			.8,  0,
			-.8,  0,
			-.8,  0,
			.8,  0,
			.8,  0,
		]
		const buffer2=gl.createBuffer()
		const positions2=[
			.8, 0, // 1st rect 1st triangle
			.8,0,
			.2,0,
			
			.2,0,  // 1st rect 2nd triangle
			.8,0,
			.2,0,
		]
		
		gl.bindBuffer(gl.ARRAY_BUFFER,buffer)
  		gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
		gl.enableVertexAttribArray(a_position)
		
		gl.vertexAttribPointer(a_position,2,gl.FLOAT,false,0,0)
		
		gl.bindBuffer(gl.ARRAY_BUFFER,buffer2)
  		gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions2), gl.STATIC_DRAW);
		gl.enableVertexAttribArray(a_position2)
		gl.vertexAttribPointer(a_position2,2,gl.FLOAT,false,0,0)

		setInterval(() => {
		gl.uniform1f(isB,!positions[0]?true:false)
			positions[0]=positions[0]===-0.8?0:-0.8
			
			gl.bindBuffer(gl.ARRAY_BUFFER,buffer)
			gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
			
			positions2[0]=positions2[0]===0.8?0:0.8
			gl.bindBuffer(gl.ARRAY_BUFFER,buffer2)
			gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions2), gl.STATIC_DRAW);
			gl.drawArrays(gl.TRIANGLES,0,6)
		}, 1000);
	</script>
</body>
</html>